package org.dingfugui.triple.serialize.jdk;

import org.dingfugui.triple.serialize.Serialize;

import java.io.*;

/**
 * Created by fugui.ding on 2015/3/22.
 */
public class JdkSerialize implements Serialize {
    @Override
    public byte[] serialize(Object obj) {

        try {
            ByteArrayOutputStream abos = new ByteArrayOutputStream();
            ObjectOutputStream oos = new ObjectOutputStream(abos);
            oos.writeObject(obj);
            return abos.toByteArray();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return new byte[0];
    }

    @Override
    public Object deserialize(byte[] data) {

        ObjectInputStream oos = null;
        try {
            oos = new ObjectInputStream(new ByteArrayInputStream(data));
            return oos.readObject();
        } catch (IOException e) {
            e.printStackTrace();
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        }
        return null;
    }

}
